OAuth (Open Authorization) হলো একটি জনপ্রিয় অথেনটিকেশন এবং অথোরাইজেশন প্রোটোকল, যা তৃতীয় পক্ষের অ্যাপ্লিকেশনকে ব্যবহারকারীর ব্যক্তিগত তথ্য অ্যাক্সেস করার অনুমতি দেয়, তবে তাদের পাসওয়ার্ড সরাসরি শেয়ার না করেই। OAuth মূলত ব্যবহারকারীদের ডেটা নিরাপদে শেয়ার করতে ব্যবহৃত হয়, বিশেষ করে API-র মাধ্যমে। এই প্রক্রিয়া API Authentication এবং Authorization নিয়ে কাজ করতে সাহায্য করে।
এই টিউটোরিয়ালে OAuth এবং API Authentication Integration ব্যাখ্যা করা হবে এবং কিভাবে API-তে OAuth ইন্টিগ্রেশন করতে হয় তাও আলোচনা করা হবে।
OAuth এর বেসিক ধারণা
OAuth সাধারণত তিনটি পাখির মধ্যে কাজ করে:
- Resource Owner (ব্যবহারকারী): যিনি অনুমতি দিচ্ছেন যে তার তথ্য অ্যাক্সেস করা হবে।
- Client (অ্যাপ্লিকেশন): যেটি API বা ডেটা অ্যাক্সেস করার চেষ্টা করছে।
- Authorization Server: যে সার্ভারটি ব্যবহারকারীকে অথোরাইজেশনের জন্য একটি টোকেন প্রদান করে।
OAuth Workflow:
- Client Application (অ্যাপ্লিকেশন) ব্যবহারকারীকে Authorization Server-এ রিডিরেক্ট করে।
- ব্যবহারকারী তাদের Credentials প্রদান করে।
- Authorization Server ব্যবহারকারীকে অথোরাইজ করে এবং একটি Authorization Code প্রদান করে।
- অ্যাপ্লিকেশন এই Authorization Code ব্যবহার করে Access Token প্রাপ্ত করে।
- অ্যাপ্লিকেশন Access Token ব্যবহার করে API-তে নিরাপদে ডেটা অ্যাক্সেস করে।
OAuth এবং API Authentication Integration
API-তে OAuth Authentication Integration করার জন্য সাধারণত দুটি স্টেপ ব্যবহার করা হয়:
- Authorization Request: এই প্রক্রিয়ায় ক্লায়েন্ট অ্যাপ্লিকেশনটি ব্যবহারকারীকে অথোরাইজেশন সার্ভারে রিডিরেক্ট করে।
- Access Token Request: ক্লায়েন্ট অ্যাপ্লিকেশনটি Authorization Code ব্যবহার করে Access Token গ্রহণ করে এবং API-তে ব্যবহারকারীর ডেটা অ্যাক্সেস করতে থাকে।
Step 1: Authorization Request
এখানে ক্লায়েন্ট অ্যাপ্লিকেশনটি ব্যবহারকারীকে OAuth Authorization Server-এ রিডিরেক্ট করে, যেখানে ব্যবহারকারী তার Credentials প্রদান করবে।
OAuth Authorization Request Example:
https://authorization-server.com/auth
?response_type=code
&client_id=your-client-id
&redirect_uri=https://your-app.com/callback
&scope=read write
&state=random-string
- response_type=code: Authorization Code চাওয়া হচ্ছে।
- client_id: অ্যাপ্লিকেশনটির শনাক্তকারী।
- redirect_uri: যেখানে ব্যবহারকারী রিডিরেক্ট হবে।
- scope: অ্যাপ্লিকেশনটি কোন ডেটার অ্যাক্সেস চাচ্ছে তা নির্দিষ্ট করে।
- state: CSRF (Cross-Site Request Forgery) সুরক্ষা।
Step 2: Access Token Request
ব্যবহারকারী সফলভাবে অথোরাইজেশন দিলে, Authorization Server একটি Authorization Code পাঠাবে। এই কোডটি Access Token প্রাপ্ত করতে ব্যবহার করা হয়।
Access Token Request Example:
POST https://authorization-server.com/token
Content-Type: application/x-www-form-urlencoded
grant_type=authorization_code
&code=received-authorization-code
&redirect_uri=https://your-app.com/callback
&client_id=your-client-id
&client_secret=your-client-secret
- grant_type=authorization_code: এখানে Authorization Code দেওয়া হয়েছে।
- code: Authorization Server থেকে প্রাপ্ত Authorization Code।
- client_id এবং client_secret: অ্যাপ্লিকেশনটির আইডি এবং সিক্রেট।
- redirect_uri: রিডিরেক্ট URI, যা আগেই নির্ধারিত ছিল।
Response:
{
"access_token": "access_token_value",
"token_type": "bearer",
"expires_in": 3600,
"refresh_token": "refresh_token_value"
}
- access_token: API অ্যাক্সেসের জন্য টোকেন।
- refresh_token: টোকেনটি এক্সপায়ার হলে নতুন টোকেন পাওয়ার জন্য ব্যবহৃত হয়।
Step 3: Accessing the API with the Access Token
এখন, অ্যাপ্লিকেশনটি Access Token ব্যবহার করে API থেকে ডেটা রিট্রাইভ করতে পারে।
API Request Example:
GET https://api.example.com/data
Authorization: Bearer access_token_value
- Authorization Header-এ Bearer token ব্যবহার করে অ্যাপ্লিকেশনটি API-তে অ্যাক্সেস পায়।
Step 4: Refreshing the Access Token (If Required)
যেহেতু Access Token সাধারণত একটি নির্দিষ্ট সময়ের জন্য বৈধ থাকে, তাই এটি এক্সপায়ার হলে Refresh Token ব্যবহার করে নতুন Access Token পাওয়া যায়।
Refresh Token Request Example:
POST https://authorization-server.com/token
Content-Type: application/x-www-form-urlencoded
grant_type=refresh_token
&refresh_token=received-refresh-token
&client_id=your-client-id
&client_secret=your-client-secret
Response:
{
"access_token": "new-access-token",
"token_type": "bearer",
"expires_in": 3600
}
OAuth Integration in .NET with IdentityServer
.NET-এ OAuth Integration করতে, আপনি IdentityServer ব্যবহার করতে পারেন, যা একটি ওপেন সোর্স OAuth 2.0 এবং OpenID Connect সার্ভার।
উদাহরণ: IdentityServer Integration
- Install IdentityServer4 NuGet Package:
dotnet add package IdentityServer4
- Configure IdentityServer:
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddIdentityServer()
.AddInMemoryClients(Config.GetClients()) // Clients Configuration
.AddInMemoryApiScopes(Config.GetApiScopes()) // Scopes Configuration
.AddInMemoryIdentityResources(Config.GetIdentityResources()) // Resources Configuration
.AddTestUsers(Config.GetUsers()) // Users Configuration
.AddDeveloperSigningCredential();
}
}
- API Authentication with OAuth:
public class ConfigureServices
{
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication("Bearer")
.AddJwtBearer("Bearer", options =>
{
options.Authority = "https://authorization-server.com";
options.Audience = "api1";
});
}
}
Conclusion
OAuth এবং API Authentication হল ওয়েব অ্যাপ্লিকেশনে ডেটার সুরক্ষা এবং অ্যাক্সেস কন্ট্রোলের জন্য অত্যন্ত গুরুত্বপূর্ণ। এটি ব্যবহারকারীদের নিরাপদে তৃতীয় পক্ষের অ্যাপ্লিকেশনে ডেটা শেয়ার করার সুবিধা দেয়, অথচ তাদের পাসওয়ার্ড শেয়ার করতে হয় না। .NET-এ IdentityServer ব্যবহার করে OAuth প্রোটোকল ইমপ্লিমেন্ট করা যায়, যা অ্যাপ্লিকেশনগুলোর জন্য শক্তিশালী অথেনটিকেশন এবং অথোরাইজেশন প্রদান করে।
Read more